package com.sunshine.lnuplus.view.CountDown;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;
import com.sunshine.lnuplus.R;
import com.sunshine.lnuplus.utils.DatabaseHelper;
import com.sunshine.lnuplus.utils.DialogFragmentHelper;
import com.sunshine.lnuplus.utils.TimeHelper;
import com.sunshine.lnuplus.view.Main.MainActivity;
import com.sunshine.timetable.ColorUtils;

import java.text.DecimalFormat;
import java.util.List;

//这个adapter注意要转型
class CountDownRecyclerHelper extends RecyclerView.Adapter<CountDownRecyclerHelper.ViewHolder> {
    private Activity activity;
    private List<List<String>> lists;
    private ColorUtils colorUtils;
    private FragmentManager fragmentManager;
    private DialogFragmentHelper dialogFragmentHelper;

    public CountDownRecyclerHelper(Activity activity, FragmentManager fragmentManager) {
        this.activity = activity;
        this.fragmentManager = fragmentManager;
        colorUtils = new ColorUtils(activity);
    }

    public void setLists(List<List<String>> lists) {
        this.lists = lists;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_count_down_recycler, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        holder.cardView.setCardBackgroundColor(colorUtils.setButtonBackground(lists.get(position).get(0), lists.get(position).get(2)));
        holder.lessonName.setText(lists.get(position).get(0));
        if (lists.get(position).get(1).equals("")&&lists.get(position).get(3).equals("")) holder.time.setText("你还没有设置考试时间");
        else holder.time.setText("考试时间：" + lists.get(position).get(1) + "\n考试地点：" + lists.get(position).get(3) + "\n距离考试还有：" + TimeHelper.getCountDownTime(lists.get(position).get(1)) + "天");


        holder.chooseTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismissDialog();
                        if (which == 0) {
                            final int mYear = TimeHelper.getYear(), mMonth = TimeHelper.getMonth() - 1, mDayOfMonth = TimeHelper.getDay(),
                                    mHour = TimeHelper.getHour(), mMinute = TimeHelper.getMinute();
                            DatePickerDialog datePickerDialog = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
                                @Override
                                public void onDateSet(DatePicker view, final int year, final int month, final int dayOfMonth) {

                                    TimePickerDialog timePickerDialog = new TimePickerDialog(activity, new TimePickerDialog.OnTimeSetListener() {
                                        @Override
                                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                            String time = year + "-" + (month + 1) + "-" + dayOfMonth + " " + hourOfDay + ":" + new DecimalFormat("00").format(minute);   //格式化
                                            DatabaseHelper.updateCountDownToDB(activity, lists.get(position).get(0), lists.get(position).get(0), time, lists.get(position).get(2), null);
                                            setLists(CountDownViewModel.getLesson());
                                            notifyDataSetChanged();
                                            Snackbar.make(MainActivity.binding.mainCoordinatorLayout, "修改成功", Snackbar.LENGTH_SHORT).show();
                                        }
                                    }, mHour, mMinute, true);
                                    timePickerDialog.show();
                                }
                            }, mYear, mMonth, mDayOfMonth);
                            datePickerDialog.show();
                        } else if (which == 1) {
                            LayoutInflater inflater = LayoutInflater.from(activity);
                            final View dialogView = inflater.inflate(R.layout.dialog_input_exam_location, null);
                            ViewGroup viewGroup = (ViewGroup) dialogView.getParent();
                            if (viewGroup != null) viewGroup.removeAllViews();
                            DialogInterface.OnClickListener positive =  new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    EditText editText = dialogView.findViewById(R.id.editText_input_exam_location);
                                    DatabaseHelper.updateCountDownToDB(activity, lists.get(position).get(0), lists.get(position).get(0), null, null, editText.getText().toString());
                                    setLists(CountDownViewModel.getLesson());
                                    notifyDataSetChanged();
                                    Snackbar.make(MainActivity.binding.mainCoordinatorLayout, "修改成功", Snackbar.LENGTH_SHORT).show();
                                }
                            };
                            DialogInterface.OnClickListener negative = new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which) { }};
                            DialogFragmentHelper dialogFragmentHelper = DialogFragmentHelper.newInstance("请输入考试地点","确定","取消",dialogView,true,positive,negative);
                            dialogFragmentHelper.show(fragmentManager,"");
                        }
                    }
                };
                dialogFragmentHelper = DialogFragmentHelper.newInstance("请选择设置项",null,null,true,new String[]{"考试时间","考试地点"},onClickListener,null,null);
                dialogFragmentHelper.show(fragmentManager,"");
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    private void dismissDialog(){
        dialogFragmentHelper.dismiss();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        TextView lessonName, time;
        Button chooseTime;

        public ViewHolder(View view) {
            super(view);
            cardView = view.findViewById(R.id.registration_recycler_item_card);
            lessonName = view.findViewById(R.id.count_down_item_lesson_name);
            time = view.findViewById(R.id.count_down_item_time);
            chooseTime = view.findViewById(R.id.count_down_item_button);
        }
    }
}